/*
* Copyright (c) 2011 Connection Road.  All rights reserved.
* This program and the accompanying material are made available
* under the terms of the Eclipse Public Licnese v1.0 which is
* available at http://www.eclipse.org/legal/epl-v10.html
*/

using Microsoft.Synchronization;
using Microsoft.Synchronization.Files;

namespace CrSyncFilesUtils
{
    /// <summary>
    /// Model class for holding various information on the affected file.
    /// </summary>
    public class CrSyncedFileInfo
    {

        public CrSyncedFileInfo(FileSyncPairInfo folderPair)
        {
            FolderPair = folderPair;
        }



        public CrSyncedFileInfo(FileSyncPairInfo folderPair,string sourceRootDirectory, string sourceRelativeFilePath, string sourceLastModified, string destinationRootDirectory, string destinationRelativeFilePath, string destinationLastModified, ChangeType appliedChangeType, long? totalBytesCopied)
        {
            SourceRootDirectory = sourceRootDirectory;
            SourceRelativeFilePath = sourceRelativeFilePath;
            SourceLastModified = sourceLastModified;
            DestinationRootDirectory = destinationRootDirectory;
            DestinationRelativeFilePath = destinationRelativeFilePath;
            DestinationLastModified = destinationLastModified;
            AppliedChangeType = appliedChangeType;
            TotalBytesCopied = totalBytesCopied;
            FolderPair = folderPair;
        }

        public CrSyncedFileInfo(FileSyncPairInfo folderPair, SyncProvider sourceProvider, SyncProvider destinationProvider)
        {
            SourceProvider = sourceProvider;
            DestinationProvider = destinationProvider;
            FolderPair = folderPair;
        }

        public CrSyncedFileInfo(FileSyncPairInfo folderPair, SyncProvider sourceProvider, SyncProvider destinationProvider, string sourcePath, string sourceFileName, string sourceLastModified, string destinationPath, string destinationFileName, string destinationLastModified, ChangeType appliedChangeType, long? totalBytesCopied)
        {
            SourceProvider = sourceProvider;
            DestinationProvider = destinationProvider;
            SourceRootDirectory = sourcePath;
            SourceRelativeFilePath = sourceFileName;
            SourceLastModified = sourceLastModified;
            DestinationRootDirectory = destinationPath;
            DestinationRelativeFilePath = destinationFileName;
            DestinationLastModified = destinationLastModified;
            AppliedChangeType = appliedChangeType;
            TotalBytesCopied = totalBytesCopied;
            FolderPair = folderPair;
        }

        public FileSyncPairInfo FolderPair { get; set; }
        public SyncProvider SourceProvider { get; set; }
        public SyncProvider DestinationProvider { get; set; }

        public string SourceRootDirectory { get; set; }
        public string SourceRelativeFilePath { get; set; }
        public string SourceLastModified { get; set; }
        public string SourceFileName { get; set; }

        public string DestinationRootDirectory { get; set; }
        public string DestinationRelativeFilePath { get; set; }
        public string DestinationLastModified { get; set; }
        public string DestinationFileName { get; set; }

        public ChangeType AppliedChangeType { get; set; }
        public long? TotalBytesCopied { get; set; }
        public bool IsDirectory { get; set; }

        public ApplyingChangeEventArgs ApplyingChangeEventArguments { get; set; }

        public FileNode GetValidFileNode()
        {
            var sourceNode = new FileNode(SourceRootDirectory).Join(SourceRelativeFilePath);
            var destinationNode = new FileNode(DestinationRootDirectory).Join(DestinationRelativeFilePath);
            
            var fileNode = sourceNode.Exists() ? sourceNode : destinationNode;
            return fileNode;
        }

        public string ChangeTypeDisplayString
        {
            get
            {

                var fileNode = GetValidFileNode();

                switch (AppliedChangeType)
                {
                    case ChangeType.Create:
                        return IsDirectory ? "Create Folder" : "New";
                    case ChangeType.Delete:
                        return IsDirectory ? "Delete Folder" : "Delete";
                    case ChangeType.Update:
                        return "Overwrite";
                    default:
                        return AppliedChangeType.ToString();
                }
            }
        }

        // For aesthetic purpose only. The path may or may not exists.
        public string GetSourceOrigin()
        {
            var fileNode = new FileNode(SourceRootDirectory).Join(SourceRelativeFilePath);
            return fileNode.GetParentPath();
        }

        // For aesthetic purpose only. The path may or may not exists.
        public string GetDestinationOrigin()
        {
            var fileNode = new FileNode(DestinationRootDirectory).Join(DestinationRelativeFilePath);
            return fileNode.GetParentPath();
        }

    }
}
